package com.sbl.bajw.pc.action;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.pc.model.LogTModel;
import com.sbl.bajw.pc.model.PatrolModel;
import com.sbl.bajw.pc.service.LogTModelService;
import com.sbl.bajw.pc.service.PatrolService;
import com.sbl.bajw.util.JsonUtil;
import com.sbl.bajw.util.LogUtil;

@Controller
@RequestMapping("/Patrol")
public class PatrolAction {

	@Resource
	private PatrolService service;

	@Resource
	private LogTModelService logService;
	
	static Logger log;
	
	static{
		log = LogUtil.getLogger("Patrol");
	}
	
	/**
	 * @标题：createApp
	 * @作者：罗琦
	 * @时间：2015年5月18日
	 * @作用：我要巡逻数据入库及位置上传时间间隔设置
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/createApp")
	public @ResponseBody String createApp(HttpServletRequest request, HttpServletResponse response) throws Exception{
		PatrolModel model = RequestUnit.getModel(PatrolModel.class);
		int num = (model.getTimeLength().intValue() * 60) / 30;
		String timer = "30~" + num;
		try {
			log.info(model.toString());
			service.updateUserLocation(model.getCreater(), timer);
		} catch (Exception e) {
			log.info(e);
			e.printStackTrace();
		}
		service.insert(model);
		
		//积分入库
		LogTModel logT = new LogTModel();
		logT.setUserid(model.getCreater());
		logT.setCode("13");
		logT.setNote(model.getTimeLength().toString());
		logService.insert(logT);
		
		return JsonResultUtil.Success();
	}
	
	/**
	 * @标题：getListApp
	 * @作者：罗琦
	 * @时间：2015年5月18日
	 * @作用：APP我要巡逻数据列表查询
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getListApp")
	public @ResponseBody String getListApp(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUnit.getString("user_id");
		List<HashMap<String, Object>> list = service.getListApp(id);
		String json = JsonResultUtil.Success(JsonUtil.ConvertArrayToJson(list));
		return json;
	}
	
	/**
	 * @标题：detailApp
	 * @作者：罗琦
	 * @时间：2015年5月18日
	 * @作用：APP巡逻详情及巡逻轨迹
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/detailApp")
	public @ResponseBody String detailApp(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = RequestUnit.getString("patrolId");
		PatrolModel model = service.getModel(id);
		String result = service.historyLocation(model);
		String json = "{\"route\":" + result + ",\"data\":" + JsonUtil.ConvertObjectToJson(model) + "}";
		return JsonResultUtil.Success(json);
	}
	
	/**
	 * @标题：checkNewPatrol
	 * @作者：罗琦
	 * @时间：2015年5月20日
	 * @作用：APP检测用户最新巡逻是否结束
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getLastPatrolInfo")
	public @ResponseBody String getLastPatrolInfo(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUnit.getString("user_id");
		PatrolModel model = service.getLastPatrolInfo(id);
		return JsonResultUtil.Success(JsonUtil.ConvertObjectToJson(model));
	}
}
